package com.wbdgj.ui.login;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/6/15.
 */

public class FindPsswordActivity extends BaseActivity{

    @BindView(R.id.login_phone_et)
    EditText login_phone_et;

    @BindView(R.id.login_psw_et)
    EditText login_psw_et;

    @BindView(R.id.login_psw_et_again)
    EditText login_psw_et_again;

    @BindView(R.id.dxCode)
    public EditText dxCode;

    @BindView(R.id.hq)
    public TextView hq;
    final int totalCount = 60;
    int time = 1;
    private boolean isClick = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_find_password;
    }

    @Override
    protected void initView() {

    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            hq.setText(String.valueOf(String.valueOf(totalCount - time))+"秒后重新发送");
            hq.setTextColor(getResources().getColor(R.color.red));
            time++;
            if (time > totalCount) {
                next();
            } else {
                startCount();
            }
        }
    };

    private void startCount() {
        getWindow().getDecorView().postDelayed(runnable, 1000);
    }

    private void next() {
        isClick = false;
        hq.setText("点击获取");
        hq.setTextColor(getResources().getColor(R.color.colorPrimary));
    }

    @OnClick(value = {R.id.login_btn, R.id.hq})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hq:
                if (isClick){
                    ToastUtils.toastShort("请稍后再点！");
                    return;
                }
                if (TextUtils.isEmpty(login_phone_et.getText().toString())){
                    ToastUtils.toastShort("手机号不能为空！");
                    return;
                }
                userscode();
                break;
            case R.id.login_btn:
                if (TextUtils.isEmpty(login_phone_et.getText().toString())){
                    ToastUtils.toastShort("请输入手机号！");
                    return;
                }
                if (TextUtils.isEmpty(login_psw_et.getText().toString())){
                    ToastUtils.toastShort("请输入密码！");
                    return;
                }
                if (TextUtils.isEmpty(login_psw_et_again.getText().toString())){
                    ToastUtils.toastShort("请输入密码！");
                    return;
                }
                if (TextUtils.isEmpty(dxCode.getText().toString())){
                    ToastUtils.toastShort("请输入验证码！");
                    return;
                }
                if (!login_psw_et.getText().toString().equals(login_psw_et_again.getText().toString())){
                    ToastUtils.toastShort("2次密码输入不一致！");
                    return;
                }
                reg();
                break;
        }
    }

    private void userscode() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().queryAuth(login_phone_et.getText().toString());
        call.enqueue(new OnResponseListener<ResultObjectModel>(this) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                    isClick = true;
                    time = 1;
                    startCount();
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void reg() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getPassword(login_phone_et.getText().toString(), dxCode.getText().toString(), login_psw_et.getText().toString());
        call.enqueue(new OnResponseListener<ResultObjectModel>(this) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                    finish();
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

}
